package com.console;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspWriter;


public class EnvConsoleService {

	public static Map<Integer, List<List<Double>>> dataCache = new HashMap<Integer, List<List<Double>>>();
	
	public static synchronized double[][] autoGeneRandomData(HttpServletRequest request, HttpServletResponse response, JspWriter out) {
		int count = Integer.parseInt(request.getParameter("count"));
		int returnCount = count;
		List<List<Double>> data = null;
		List<List<Double>> prevData = dataCache.get(count - 1);
		if (prevData == null) {
			if(dataCache.get(count) == null) {
				//初次加载
				data = new ArrayList<List<Double>>();
				List<Double> newData = autoGeneRandomData();
				List<Double> currData1 = new ArrayList<Double>();
				List<Double> currData2 = new ArrayList<Double>();
				List<Double> currData3 = new ArrayList<Double>();
				List<Double> currData4 = new ArrayList<Double>();
				currData1.add(newData.get(0));
				currData2.add(newData.get(1));
				currData3.add(newData.get(2));
				currData4.add(newData.get(3));
				data.add(currData1);
				data.add(currData2);
				data.add(currData3);
				data.add(currData4);
				dataCache.put(count, data);
			}else {
				//轮回加载
				returnCount = 24;
				prevData = dataCache.get(24);
				if(prevData != null) {
					List<Double> newData = autoGeneRandomData();
					List<Double> prevData1 = prevData.get(0);
					List<Double> prevData2 = prevData.get(1);
					List<Double> prevData3 = prevData.get(2);
					List<Double> prevData4 = prevData.get(3);
					prevData1.remove(0);
					prevData2.remove(0);
					prevData3.remove(0);
					prevData4.remove(0);
					
					prevData1.add(newData.get(0));
					prevData2.add(newData.get(1));
					prevData3.add(newData.get(2));
					prevData4.add(newData.get(3));
					data = prevData;
					dataCache.remove(count);
					dataCache.put(count, data);
				}
			}
		} else {
			if(dataCache.get(count) == null) {
				data = new ArrayList<List<Double>>();
				List<Double> newData = autoGeneRandomData();
				List<Double> prevData1 = prevData.get(0);
				List<Double> prevData2 = prevData.get(1);
				List<Double> prevData3 = prevData.get(2);
				List<Double> prevData4 = prevData.get(3);
				
				prevData1.add(newData.get(0));
				prevData2.add(newData.get(1));
				prevData3.add(newData.get(2));
				prevData4.add(newData.get(3));
				data = prevData;
				dataCache.put(count, data);
			}else {
				returnCount = 24;
				data = new ArrayList<List<Double>>();
				List<Double> newData = autoGeneRandomData();
				List<Double> prevData1 = prevData.get(0);
				List<Double> prevData2 = prevData.get(1);
				List<Double> prevData3 = prevData.get(2);
				List<Double> prevData4 = prevData.get(3);
				prevData1.remove(0);
				prevData2.remove(0);
				prevData3.remove(0);
				prevData4.remove(0);
				
				prevData1.add(newData.get(0));
				prevData2.add(newData.get(1));
				prevData3.add(newData.get(2));
				prevData4.add(newData.get(3));
				data = prevData;
				dataCache.put(count, data);
			}
		}
		double[][] returnData = new double[4][returnCount];
		if(data != null) {
			for (int i = 0; i < data.size(); i++) {
				List<Double> subData = data.get(i);
				for(int j = 0; j < subData.size(); j++) {
					returnData[i][j] = subData.get(j);
				}
			}
		}
		return returnData;
	}
	
	
	public static String[] getLabels(HttpServletRequest request, HttpServletResponse response, JspWriter out) {
		int count = Integer.parseInt(request.getParameter("count"));
		List<String> labelList = new ArrayList<String>();
		labelList.add("01:00");
		labelList.add("02:00");
		labelList.add("03:00");
		labelList.add("04:00");
		labelList.add("05:00");
		labelList.add("06:00");
		labelList.add("07:00");
		labelList.add("08:00");
		labelList.add("09:00");
		labelList.add("10:00");
		labelList.add("11:00");
		labelList.add("12:00");
		labelList.add("13:00");
		labelList.add("14:00");
		labelList.add("15:00");
		labelList.add("16:00");
		labelList.add("17:00");
		labelList.add("18:00");
		labelList.add("19:00");
		labelList.add("20:00");
		labelList.add("21:00");
		labelList.add("22:00");
		labelList.add("23:00");
		labelList.add("24:00");
		if(dataCache.size() == 24) {
			for(int i = 0; i < count; i++) {
				String label = labelList.get(0);
				labelList.remove(0);
				labelList.add(label);
			}
		}else {
			return labelList.toArray(new String[labelList.size()]);
		}
		return labelList.toArray(new String[labelList.size()]);
	}
	
	private static List<Double> autoGeneRandomData() {
		while(true) {
			double data1 = Math.random();
			double data2 = Math.random();
			double data3 = Math.random();
			double data4 = Math.random();
			if(data1 + data2 + data3 + data4 < 1 && data1 > 0.08 && data2 > 0.07 && data3 > 0.06 && data4 > 0.1) {
				List<Double> list = new ArrayList<Double>();
				list.add(data1);
				list.add(data2);
				list.add(data3);
				list.add(data4);
				return list;
			}
		}

	}
	
	public static void main(String[] args) {
		System.out.println();
	}
}
